// vip/enterprise/index.js
import {
  toast
} from "../../utils/util";
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    record: [],
    idisplaylength: 2,
    idisplaystart: 0,
    compony: '',
    business: '',
    total: 0,
    person: 0,
    count: 2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getRecord()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    app.globalData.imgUrl = []
  },
  getRecord() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 2000
    })
    const result = this.data.record
    const {
      idisplaylength,
      idisplaystart
    } = this.data
    const state = ''
    app.$api.apply.identityFind({
      idisplaylength,
      idisplaystart,
      state
    }).then((res) => {
      const data = res.data
      wx.hideToast()
      const newArray = result.concat(data.data)
      this.setData({
        record: newArray,
        total: data.total
      })
      console.log(this.data.record)
    }).catch((res) => {
      wx.hideToast()
      toast('请稍后再试', 1000)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    app.globalData.imgUrl = []
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // const total = this.data.total
    // const record = this.data.record.length
    // if(record && record === total) {
    //   toast("没有更多", 1000)
    //   return
    // }
    // this.setData({
    //   idisplaystart: record
    // })
    // this.getRecord()
  },
  bindDelete(e) {
    const record = this.data.record
    const {id, index} = e.detail
    app.$api.apply.identityDelete({id})
      .then((res) => {
        record.splice(index, 1)
        this.setData({
          record: record
        })
        toast('删除成功', 1000)
      }).catch((res) => {
        toast('请稍后再试', 1000)
      })
  },
  bindSubmit(e) {
    const flag = this.validate()
    const record = this.data.record
    if (flag) {
      const fronturl = JSON.parse(app.globalData.imgUrl[0]).fileUrl
      const reverseurl = JSON.parse(app.globalData.imgUrl[1]).fileUrl
      const username = this.data.compony
      const ideno = this.data.business
      if(record.length) {
        const id = record[0].id
        app.$api.apply.identityUpdate({
          fronturl,
          username,
          ideno,
          reverseurl,
          id
        }).then((res) => {
          toast("提交成功", 1000)
          this.addItem()
        }).catch((res) => {
          toast('请稍后再试', 1000)
        })
      }else{
        app.$api.apply.identityAdd({
          fronturl,
          username,
          ideno,
          reverseurl
        }).then((res) => {
          toast("提交成功", 1000)
          this.addItem()
        }).catch((res) => {
          toast('请稍后再试', 1000)
        })
      }
    }
  },
  addItem() {
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 2000
    })
    const idisplaylength = 1
    const idisplaystart = 0
    const state =  ''
    const result = [...this.data.record]
    app.$api.apply.identityFind({
      idisplaylength,
      idisplaystart,
      state
    }).then((res) => {
      const data = res.data
      wx.hideToast()
      result.unshift(...data.data)
      this.setData({
        record: result,
        total: data.total
      })
    }).catch((res) => {
      wx.hideToast()
      toast('请稍后再试', 1000)
    })
  },
  validate() {
    const {
      compony,
      business
    } = this.data
    const imgUrl = app.globalData.imgUrl
    const idReg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
    console.log(imgUrl)
    if (!imgUrl.length) {
      toast("照片不能为空", 1000)
      return false
    }
    if (compony.trim() === '') {
      toast("姓名不能为空", 1000)
      return false
    }
    if (!idReg.test(business)) {
      toast("身份证号格式不正确", 1000)
      return false
    }
    return true
  },
  handleCompony(e) {
    const value = e.detail.value
    this.setData({
      compony: value
    })
  },
  handleBusiness(e) {
    const value = e.detail.value
    this.setData({
      business: value
    })
  },

  /**
   * 用户点击右上角分享
   */
})